package com.itheima.controller;

import com.itheima.pojo.Address;
import com.itheima.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Description:
 * @author codermao
 * @date 2023/11/5 20:17
 * <p>
 * <p>
 *  TODO:响应  @ResponseBody
 *      1）普通字符串
 *      2）json格式字符串
 */
@Controller
public class UserController4 {
    /**
     * 这里的返回值是请求转发的地址
     *  SpringMVC刚诞生时，还是jsp，默认是请求转发
     */
    @RequestMapping("/method01")
    public String method01(){
        System.out.println("method01");
        return "index.jsp";//底层是请求转发，封装了request.getRequestDispatcher("index.jsp").forward(request,response);
    }

    /**
     *  @ResponseBody
     *     这里返回值被放到响应体中 response.getWriter().print("method02");
     */
    @RequestMapping("/method02")
    @ResponseBody
    public String method02(){
        System.out.println("method02");
        return "method02";
    }

    /**
     * 会自动将pojo转换成json字符串（前提：导入jackson，还有@EnableWebMVC)
     */
    @RequestMapping("/method03")
    @ResponseBody
    public Object method03(){
        System.out.println("method03");

        User user = new User();
        user.setName("zhangsan");
        user.setAge(18);

        Address address = new Address();
        address.setProvince("河南");
        address.setCity("新乡");

        user.setAddress(address);
        return user;
    }
}
